//
//  changePasswordViewController.m
//  忘不了
//
//  Created by rimi on 16/7/16.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "changePasswordViewController.h"
#import "SDAutoLayout.h"
#import "BaseRequest.h"
#import "UserModel.h"
#import "CheckWeb.h"
@interface changePasswordViewController ()<UITextFieldDelegate>

@end

@implementation changePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改密码";
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self loadUI];
}

- (void)loadUI {
    NSArray *array = @[@"账号",@"密码",@"新密码"];
    for (int i=0; i<3; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = array[i];
        label.textColor = [UIColor brownColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:18];
        [self.view addSubview:label];
        label.sd_layout
        .leftSpaceToView(self.view,30)
        .rightSpaceToView(self.view,30)
        .topSpaceToView(self.view,150+self.view.frame.size.width*0.2*i+30*i)
        .heightIs(self.view.frame.size.width*0.1);
    }
    for (int j=0; j<3; j++) {
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = [UIColor blackColor];
        textField.font = [UIFont systemFontOfSize:20];
        textField.layer.cornerRadius = 10;
        textField.layer.masksToBounds = YES;
        textField.tag = 500 + j;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.backgroundColor = [UIColor whiteColor];
        if (textField.tag == 501 || textField.tag == 502) {
            textField.secureTextEntry = YES;
        }
        [self.view addSubview:textField];
        textField.sd_layout
        .leftSpaceToView(self.view,30)
        .rightSpaceToView(self.view,30)
        .heightIs(self.view.frame.size.width*0.1)
        .topSpaceToView(self.view,150+self.view.frame.size.width*0.1*(j+1)+30*j+self.view.frame.size.width*0.1*j);
    }
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeButton setTitle:@"修改" forState:UIControlStateNormal];
    changeButton.backgroundColor = [UIColor colorWithRed:0.8784 green:0.8431 blue:0.7765 alpha:1.0];
    [changeButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    changeButton.layer.cornerRadius = 10;
    changeButton.layer.borderColor = [UIColor colorWithRed:0.5727 green:0.5812 blue:0.4312 alpha:1.0].CGColor;
    changeButton.layer.borderWidth = 1;
    [changeButton addTarget:self action:@selector(changeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    changeButton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .bottomSpaceToView(self.view,self.view.frame.size.width*0.2)
    .widthIs(self.view.frame.size.width*0.3)
    .heightIs(self.view.frame.size.width*0.1);
}

- (void)changeButtonPressed {
    UserModel *user = [UserModel shareUserModel];
    UITextField *userName = [self.view viewWithTag:500];
    UITextField *password = [self.view viewWithTag:501];
    UITextField *newPassword = [self.view viewWithTag:502];
    if ([userName.text isEqual:@""] || [password.text isEqual:@""] || [newPassword.text isEqual:@""]) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请填写完整" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else if (![userName.text isEqualToString:user.userNmae] || ![password.text isEqualToString:user.password]) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else {
        BOOL isWeb = [CheckWeb checkWang];
        if (isWeb == NO) {
            UIAlertController *tip3 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [tip3 addAction:sure];
            [self presentViewController:tip3 animated:YES completion:nil];
        }else{
            [BaseRequest startModifyPasswordWithNewPassword:newPassword.text completionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    UIAlertController *tip2 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"修改密码成功" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        userName.text = @"";
                        password.text = @"";
                        newPassword.text = @"";
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    [tip2 addAction:sure];
                    [self presentViewController:tip2 animated:YES completion:nil];
                }else{
                    UIAlertController *tip3 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"修改密码失败" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                    [tip3 addAction:sure];
                    [self presentViewController:tip3 animated:YES completion:nil];
                }
            }];
            
        }
        
    }
}

//文本框限定最大长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 15) {
        return NO;
    }
    return YES;
}

//点空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
